package com.pinc.cpbl;

import java.io.InputStream;

public class Parser_Grade extends myHTMLparser {

	private String[] items;
	private int qfield;

	public Parser_Grade(InputStream istream, int i_qfield) {
		super(istream, "utf8");
		items = new String[Grade_Adapter.ITEMS];
		qfield = i_qfield;
	}

	public boolean parse(Grade_Adapter adapter)
	{
		String retStr;
		String tmpStr;
		int loop_index = 0;
		int arr_index = 0;
		int show_count = 10;
		
		if ( !checkTable())
			return false;
		
		nextTag();
		retStr = getTagName();

		while( retStr.compareTo("/table") != 0 && show_count >= 0)
		{
			if ( retStr.compareTo("tr") == 0)
			{				
				if ( arr_index > 0)
				{
					adapter.add(items);
					show_count--;
				}
				loop_index = 0;
				arr_index = 0;
			}
			else if( retStr.compareTo("td") == 0 || retStr.compareTo("th") == 0)
			{
				if ( loop_index <= 4 || loop_index == qfield)
				{
					if ( retStr.compareTo("td") == 0)
						tmpStr = getContent("td");
					else
						tmpStr = getContent("th");

					tmpStr = tmpStr.replaceAll("<[^<>]+>", "").trim();
					items[arr_index++] = tmpStr;
				}
				loop_index++;
			}
			nextTag();
			retStr = getTagName();
		}
		
		if ( arr_index > 0)
		{
			adapter.add(items);
		}

		return true;
	}

}
